package com.flexrrb.controls
{
	import flash.display.DisplayObject;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;
	
	import mx.core.IFlexDisplayObject;
	import mx.core.IUITextField;
	import mx.core.UITextField;
	import mx.core.mx_internal;
	import mx.controls.CalendarLayout;
	import mx.skins.halo.DateChooserIndicator;
	import mx.styles.ISimpleStyleClient;
	use namespace mx_internal;

	public class RRBCalendarLayout extends CalendarLayout
	{
		public function RRBCalendarLayout()
		{
			super();
		}

		private var selectionIndicator:Array=[];
		private var dayNamesChanged:Boolean=false;
		private var selectedMonthYearChanged:Boolean=false;
		private var disabledArrays:Array=[];
		private var rollOverIndicator:IFlexDisplayObject;
		private var todayIndicator:IFlexDisplayObject;
		private var dayBlocksArray:Array=[];
		private var cellHeight:Number=14;
		private var cellWidth:Number=14;
		private var yOffset:Number=-1;

		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
		{
			super.updateDisplayList(unscaledWidth, unscaledHeight);

			var paddingLeft:Number=getStyle("paddingLeft");
			var paddingRight:Number=getStyle("paddingRight");
			var paddingBottom:Number=getStyle("paddingBottom");
			var paddingTop:Number=getStyle("paddingTop");

			var blockX:Number=paddingLeft;

			// Bug 134794, clip height/width so that RTE's are not thrown
			cellWidth=Math.max((unscaledWidth - (paddingLeft + paddingRight)) / 7, 4);
			cellHeight=Math.max((unscaledHeight - paddingBottom) / 7, 4);

			var labelPosition:Number=paddingTop;

			rollOverIndicator.setActualSize(cellWidth, cellHeight);
			todayIndicator.setActualSize(cellWidth, cellHeight);

			for (var columnIndex:int=0; columnIndex < 7; columnIndex++)
			{
				// Remember the height of the cells if not set by user.
				// Create the 7 labels within each DayBlock.
				// The first row in each column displays a day name string, such as "Sun".
				// The other six rows displays day numbers in the range 1-31.

				for (var rowIndex:int=0; rowIndex < 7; rowIndex++)
				{
					var label:IUITextField=dayBlocksArray[columnIndex][rowIndex];
					if (rowIndex == 0)
						labelPosition=paddingTop;
					else
						labelPosition+=cellHeight;

					label.setActualSize(cellWidth, cellHeight);
					label.move(blockX, labelPosition);

					if (selectionIndicator[columnIndex][rowIndex])
					{
						selectionIndicator[columnIndex][rowIndex].setActualSize(cellWidth, cellHeight);
						selectionIndicator[columnIndex][rowIndex].move(blockX, labelPosition + yOffset);
					}
						//label.width = cellWidth;
						//label.height = cellHeight;
						//label.x = blockX;
						//label.y = labelPosition;
				}

				blockX+=cellWidth;
			}

			//drawDayNames();
			//setSelectedMonthAndYear();
		}

		/*private function drawDayNames():void
		{
			for (var columnIndex:int=0; columnIndex < 7; columnIndex++)
			{
				var dayOfWeek:int=(columnIndex + firstDayOfWeek) % 7;
				// dayNames will be null if there are no resources.
				var dayName:String=dayNames ? dayNames[dayOfWeek] : "";
				dayBlocksArray[columnIndex][0].text=dayName;
			}
		}*/

		override protected function createChildren():void
		{


			var labelPosition:Number=0;

			createLocalDayLabels(-1);
			createLocalTodayIndicator(0);

			if (!rollOverIndicator)
			{
				var rollOverIndicatorClass:Class=getStyle("rollOverIndicatorSkin");
				if (!rollOverIndicatorClass)
					rollOverIndicatorClass=DateChooserIndicator;
				rollOverIndicator=IFlexDisplayObject(new rollOverIndicatorClass());
				// too lazy to make an interface for this.
				if (isDateChooserIndicator(rollOverIndicator))
					Object(rollOverIndicator).indicatorColor="rollOverColor";
				if (rollOverIndicator is ISimpleStyleClient)
					ISimpleStyleClient(rollOverIndicator).styleName=this;
				addChildAt(DisplayObject(rollOverIndicator), 0);
				rollOverIndicator.visible=false;
			}

			// Wait for all of the properties to be set before calling setSelectedMonthAndYear
			dayNamesChanged=true;
			selectedMonthYearChanged=true;
			super.createChildren();			
		}

		private function createLocalDayLabels(childIndex:int):void
		{
			var weekDayStyleName:Object=getStyle("weekDayStyleName");

			// Remember the height of the cells if not set by user.
			// Create the 7 labels within each DayBlock.
			// The first row in each column displays a day name string,
			// such as "Sun".
			// The other six rows displays day numbers in the range 1-31.

			// Calendar days
			for (var columnIndex:int=0; columnIndex < 7; columnIndex++)
			{
				dayBlocksArray[columnIndex]=[];
				selectionIndicator[columnIndex]=[];

				for (var rowIndex:int=0; rowIndex < 7; rowIndex++)
				{
					var label:IUITextField=dayBlocksArray[columnIndex][rowIndex]=IUITextField(createInFontContext(UITextField));

					label.selectable=false;
					label.ignorePadding=true;

					if (childIndex == -1)
						addChild(DisplayObject(label));
					else
						addChildAt(DisplayObject(label), childIndex++);

					if (rowIndex == 0)
					{
						label.styleName=weekDayStyleName ? weekDayStyleName : this;
					}
					else
					{
						label.styleName=this;
					}
					label.setColor(0xFF0000);
				}

				disabledArrays[columnIndex]=new Array(7);
			}
		}
		static private var dcis:Object={};

		static private function isDateChooserIndicator(parent:Object):Boolean
		{
			var s:String=getQualifiedClassName(parent);
			if (dcis[s] == 1)
				return true;

			if (dcis[s] == 0)
				return false;

			if (s == "mx.skins.halo::DateChooserIndicator")
			{
				dcis[s] == 1;
				return true;
			}

			var x:XML=describeType(parent);
			var xmllist:XMLList=x.extendsClass.(@type == "mx.skins.halo::DateChooserIndicator");
			if (xmllist.length() == 0)
			{
				dcis[s]=0;
				return false;
			}

			dcis[s]=1;
			return true;
		}

		private function createLocalTodayIndicator(childIndex:int):void
		{
			if (!todayIndicator)
			{
				var todayIndicatorClass:Class=getStyle("todayIndicatorSkin");
				if (!todayIndicatorClass)
					todayIndicatorClass=DateChooserIndicator;
				todayIndicator=IFlexDisplayObject(new todayIndicatorClass());

				if (isDateChooserIndicator(todayIndicator))
				{
					Object(todayIndicator).indicatorColor="todayColor";
				}
				if (todayIndicator is ISimpleStyleClient)
					ISimpleStyleClient(todayIndicator).styleName=this;

				addChildAt(DisplayObject(todayIndicator), childIndex);

				todayIndicator.visible=false;
			}
		}

		override protected function commitProperties():void
		{
		/*if (hasFontContextChanged() && todayIndicator != null)
		   {
		   // Re-create the children so we can display
		   // the embedded font from the new font context.
		   removeSelectionIndicators();

		   var childIndex:int = getChildIndex(DisplayObject(todayIndicator));
		   removeTodayIndicator();
		   createTodayIndicator(childIndex);

		   childIndex = getChildIndex(dayBlocksArray[0][0]);
		   removeDayLabels();
		   createDayLabels(childIndex);

		   enabledChanged = true;
		   dayNamesChanged = true;
		   selectedMonthYearChanged = true;
		   }

		   if (enabledChanged)
		   {
		   enabledChanged = false;

		   for (var o:int = 0; o < 7; o++)
		   {
		   for (var r:int = 0; r < 7; r++)
		   {
		   dayBlocksArray[o][r].enabled = _enabled;
		   disabledArrays[o][r] = _enabled;
		   }
		   }

		   if (!_enabled)
		   {

		   if (todayIndicator)
		   todayIndicator.alpha = 0.3;

		   // Remove the mouse event listeners
		   removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		   removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		   removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		   removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		   }
		   else
		   {
		   if (todayIndicator)
		   todayIndicator.alpha = 1.0;

		   selectedMonthYearChanged = true;

		   // Restore the mouse event listeners
		   addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
		   addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
		   addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
		   addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
		   }
		   }

		   if (dayNamesChanged)
		   {
		   dayNamesChanged = false;

		   drawDayNames();
		   }

		   if (selectedMonthYearChanged)
		   {
		   selectedMonthYearChanged = false;

		   var proposedDate:Date =
		   new Date(_proposedDisplayedYear == -1 ? _displayedYear : _proposedDisplayedYear,
		   _proposedDisplayedMonth == -1 ? _displayedMonth : _proposedDisplayedMonth);

		   if (isDateInRange(proposedDate, _selectableRange, selRangeMode, true))
		   {
		   setSelectedMonthAndYear();
		   }

		   _proposedDisplayedYear = -1;
		   _proposedDisplayedMonth = -1;
		 }*/
		}

	}
}